﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;
using HZ.Supervise.DAL;

namespace HZ.Supervise.Web.Secondphase.ReportArea
{
	public partial class AreaGoodsDetail : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
				SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
				if (!string.IsNullOrEmpty(sq.Mid)) //如果月份为空 参数有误
					if (true)
					{

						BindList();
					}
			}
		}

		/// <summary>
		/// 数据查询和绑定
		/// </summary>
		void BindList()
		{
			SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
			string month = sq.Mid;
			sq.Mid = "";
            string  Sort =Convert.ToString(sq.SortSource);
			int t = 0;
			IList<HZ.Supervise.Model.Supervise> CompanyList;
			HZ.Supervise.BLL.BLL_Statistics Bll_sta = (HZ.Supervise.BLL.BLL_Statistics)HZ.Supervise.BLLFactory.IBLL_Statistics();
			sq.ProcureCatalogID = sq.ProcureCatalogID;
			sq.Append = Function.FormatPlanTime("PlanTime", month);
			sq.SortFields = "ProductName ASC";
            if (sq.SortSource == 0)
            {
                sq.Append += "and StockType=0";
            }
            if (sq.SortSource == 1)
            {
                sq.Append += "and StockType=1";
            }
            if (sq.SortSource == 2)
            {
                sq.Append += "and StockType=2";
            }
			sq.Append += " and OrderDetailState >0";
            sq.SortSource = null;
			string areaname = sq.AreaName;
			sq.AreaName = null;
			CompanyList = Bll_sta.GetTradeInfo(sq, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
            DataTable dt = HZ.Data.DAL_Helper.GetDataTableFromModel<HZ.Supervise.Model.Supervise>(CompanyList);
            HZ.Supervise.Web.Function.ChangePSJE(dt, "DistributeCount", "Price");
			sq.AreaName = areaname;
			AspNetPager1.RecordCount = t;
			sq.Mid = month;
            sq.SortSource =Convert.ToInt32(Sort);
			this.gvwDetail.DataSource = dt;
			this.gvwDetail.DataBind();

		}

		/// <summary>
		/// 查询事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void btnQuery_Click(object sender, EventArgs e)
		{
			SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

			sq.Pack = DAL_Helper.Replace(this.txtPack.Text.Trim());

			sq.ProductName = DAL_Helper.Replace(this.txtProductName.Text.Trim());

			sq.MedicineModelName = DAL_Helper.Replace(this.txtMedicineModelName.Text.Trim());

			sq.Unit = DAL_Helper.Replace(this.txtUnit.Text.Trim());

            sq.OrderName = DAL_Helper.Replace(this.txtPurchaseName.Text.Trim());

			AspNetPager1.CurrentPageIndex = 1;

			BindList();

		}

		/// <summary>
		/// 合并表头
		/// by张雄  2011.5.7
		/// </summary>
		/// <param name="sender">事件发送源</param>
		/// <param name="e">事件参数</param>
		protected void gvwDetail_RowCreated(object sender, GridViewRowEventArgs e)
		{
			switch (e.Row.RowType)
			{
				case DataControlRowType.Header:
					//第一行表头
					TableCellCollection tcHeader = e.Row.Cells;
					tcHeader.Clear();
					tcHeader.Add(new TableHeaderCell());
					tcHeader[0].Attributes.Add("colspan", "18");
					tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
					SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

					if (!string.IsNullOrEmpty(sq.AreaName))
					{

						tcHeader[0].Text = HZ.Supervise.Web.Function.FormartDate(sq.Mid) + " " + sq.AreaName + "</th></tr><tr>";

					}
					else
					{

						tcHeader[0].Text = HZ.Supervise.Web.Function.FormartDate(sq.Mid) + "</th></tr><tr>";

					}
					//第2行表头
					tcHeader.Add(new TableHeaderCell());
					tcHeader[1].Attributes.Add("bgcolor", "Khaki");
					tcHeader[1].Text = "序号";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[2].Attributes.Add("bgcolor", "Khaki");
					tcHeader[2].Text = "采购数量";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[3].Attributes.Add("bgcolor", "Khaki");
					tcHeader[3].Text = "配送数量";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[4].Attributes.Add("bgcolor", "Khaki");
					tcHeader[4].Text = "入库数量";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[5].Attributes.Add("bgcolor", "Khaki");
					tcHeader[5].Text = "采购总金额";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[6].Attributes.Add("bgcolor", "Khaki");
					tcHeader[6].Text = "入库总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[7].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[7].Text = "配送总金额";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[8].Attributes.Add("bgcolor", "Khaki");
					tcHeader[8].Text = "采购单名称";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[9].Attributes.Add("bgcolor", "Khaki");
					tcHeader[9].Text = "开始时间";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[10].Attributes.Add("bgcolor", "Khaki");
					tcHeader[10].Text = "截至时间";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[11].Attributes.Add("bgcolor", "Khaki");
					tcHeader[11].Text = "通用名";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[12].Attributes.Add("bgcolor", "Khaki");
					tcHeader[12].Text = "剂型";
				
					tcHeader.Add(new TableHeaderCell());
					tcHeader[13].Attributes.Add("bgcolor", "Khaki");
					tcHeader[13].Text = "采购规格";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[14].Attributes.Add("bgcolor", "Khaki");
					tcHeader[14].Text = "采购单位";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[15].Attributes.Add("bgcolor", "Khaki");
					tcHeader[15].Text = "实际采购价";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[16].Attributes.Add("bgcolor", "Khaki");
					tcHeader[16].Text = "生产企业";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[17].Attributes.Add("bgcolor", "Khaki");
					tcHeader[17].Text = "配送企业";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[18].Attributes.Add("bgcolor", "Khaki");
					tcHeader[18].Text = "采购医院";

					break;
			}
		}

		/// <summary>
		/// 分页
		/// </summary>
		/// <param name="sender">事件的发送源</param>
		/// <param name="e">事件的参数</param>
		protected void AspNetPager1_PageChanged(object sender, EventArgs e)
		{
			BindList();
		}

		protected void btnExcel_Click(object sender, EventArgs e)
		{
			SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
			string month = sq.Mid;
			sq.Mid = "";
            string Sort = Convert.ToString(sq.SortSource);
			int t = 0;
			IList<HZ.Supervise.Model.Supervise> CompanyList;
			HZ.Supervise.BLL.BLL_Statistics Bll_sta = (HZ.Supervise.BLL.BLL_Statistics)HZ.Supervise.BLLFactory.IBLL_Statistics();
			sq.ProcureCatalogID = sq.ProcureCatalogID;
			sq.Append = Function.FormatPlanTime("PlanTime", month);
            sq.SortFields = "ProductName ASC";
            if (sq.SortSource == 0)
            {
                sq.Append += "and StockType=0";
            }
            if (sq.SortSource == 1)
            {
                sq.Append += "and StockType=1";
            }
            if (sq.SortSource == 2)
            {
                sq.Append += "and StockType=2";
            }
            sq.Append += " and OrderDetailState >0";
            sq.SortSource = null;
			string areaname = sq.AreaName;
            sq.SortSource = null;
			sq.AreaName = null;
			CompanyList = Bll_sta.GetTradeInfo(sq, 1, 0, out t);
			DataTable dt = HZ.Data.DAL_Helper.GetDataTableFromModel<HZ.Supervise.Model.Supervise>(CompanyList);
			sq.AreaName = areaname;
			AspNetPager1.RecordCount = t;
            sq.SortSource = Convert.ToInt32(Sort);
            HZ.Supervise.Web.Function.ChangePSJE(dt, "DistributeCount", "Price");
			HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwDetail);
		}
	}
}
